<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item v-if="isgly" label="商户号" prop="merchantCode">
        <el-input
          v-model="queryParams.merchantCode"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询单号" prop="requestSn">
        <el-input
          v-model="queryParams.requestSn"
          placeholder="请输入查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口名称" prop="interName">
        <el-input
          v-model="queryParams.interName"
          placeholder="请输入接口名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调用关键字" prop="interKeyword">
        <el-input
          v-model="queryParams.interKeyword"
          placeholder="请输入接口调用关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询结果" prop="queryResult">
        <el-input
          v-model="queryParams.queryResult"
          placeholder="请输入查询结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="收费结果" prop="costResult">
        <el-input
          v-model="queryParams.costResult"
          placeholder="请输入收费结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column v-if="isgly" label="商户号" align="center" prop="merchantCode"/>
      <el-table-column label="查询单号" align="center" prop="requestSn"/>
      <el-table-column label="接口名称" align="center" prop="interName"/>
      <el-table-column label="调用关键字" align="center" prop="interKeyword"/>
      <el-table-column label="查询结果" align="center" prop="queryResult"/>
      <el-table-column label="收费时间" align="center" prop="chargeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="扣费额" align="center" prop="costNum"/>
      <el-table-column label="收费结果" align="center" prop="costResult"/>
      <el-table-column label="剩余额度" align="center" prop="balance"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  </div>
</template>

<script>
  import {listRecord, getRecord, addRecord, updateRecord} from "@/api/selectRecord/selectRecord";

  export default {
    name: "Record",
    data() {
      return {
        isgly: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 查询收费表格数据
        recordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merchantCode: null,
          requestSn: null,
          interName: null,
          interKeyword: null,
          queryResult: null,
          costResult: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          merchantCode: [
            {required: true, message: "商户号不能为空", trigger: "blur"}
          ],
          merchantOrderNumber: [
            {required: true, message: "商户侧订单号不能为空", trigger: "blur"}
          ],
          interName: [
            {required: true, message: "接口编号不能为空", trigger: "blur"}
          ],
          interKeyword: [
            {required: true, message: "接口调用关键字不能为空", trigger: "blur"}
          ],
          queryResult: [
            {required: true, message: "查询结果不能为空", trigger: "blur"}
          ],
          costNum: [
            {required: true, message: "扣费额不能为空", trigger: "blur"}
          ],
          costResult: [
            {required: true, message: "收费结果不能为空", trigger: "blur"}
          ],
          balance: [
            {required: true, message: "剩余额度不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      var roles = this.$store.state.user.roles;
      for (var i = 0; i < roles.length; i++) {
        if (roles[i] == 'admin' || roles[i] == 'yewuadmin') {
          this.isgly = true
          break
        }
      }
      this.getList();
    },
    methods: {
      /** 查询查询收费列表 */
      getList() {
        this.loading = true;
        listRecord(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          merchantCode: null,
          merchantOrderNumber: null,
          interName: null,
          interKeyword: null,
          queryResult: null,
          costResult: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getRecord(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改查询收费";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/record/export', {
          ...this.queryParams
        }, `record_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
